<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.cardId"
          clearable
          placeholder="会员卡ID"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.memberId"
          clearable
          placeholder="会员ID"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.paymentMethod"
          clearable
          placeholder="支付方式"
          class="filter-item"
          style="width: 130px"
          @change="crud.toQuery"
        >
          <el-option label="现金" value="CASH" />
          <el-option label="微信" value="WECHAT" />
          <el-option label="支付宝" value="ALIPAY" />
        </el-select>
        <el-date-picker
          v-model="query.createTime"
          :default-time="['00:00:00','23:59:59']"
          type="daterange"
          range-separator=":"
          size="small"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="cardId" label="会员卡ID" />
      <el-table-column prop="memberId" label="会员ID" />
      <el-table-column prop="amount" label="充值金额" />
      <el-table-column prop="giftAmount" label="赠送金额" />
      <el-table-column prop="beforeAmount" label="充值前余额" />
      <el-table-column prop="afterAmount" label="充值后余额" />
      <el-table-column prop="paymentMethod" label="支付方式">
        <template slot-scope="scope">
          {{ getPaymentMethodLabel(scope.row.paymentMethod) }}
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="操作人" />
      <el-table-column prop="createTime" label="创建时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="remarks" label="备注" />
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import { getRechargeRecords } from '@/api/vip/rechargeRecord'
import CRUD, { presenter, header } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'

const defaultQuery = { cardId: null, memberId: null, paymentMethod: null, createTime: null }

export default {
  name: 'RechargeRecord',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header()],
  cruds() {
    return CRUD({
      title: '充值记录',
      url: 'api/rechargeRecords',
      sort: 'id,desc',
      crudMethod: { ...getRechargeRecords }
    })
  },
  data() {
    return {
      permission: {
        list: ['rechargeRecord:list']
      },
      rules: {},
      queryTypeOptions: [
        { key: 'cardId', display_name: '会员卡ID' },
        { key: 'memberId', display_name: '会员ID' },
        { key: 'createBy', display_name: '操作人' }
      ]
    }
  },
  methods: {
    getPaymentMethodLabel(method) {
      const methodMap = {
        'CASH': '现金',
        'WECHAT': '微信',
        'ALIPAY': '支付宝'
      }
      return methodMap[method] || method
    },
    // 钩子：在获取表格数据之前执行
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>
  .date-item {
    width: 185px;
    margin-right: 10px;
    margin-bottom: 5px;
  }
</style> 